# -*- encoding: utf8 -*-
import datetime

from django import forms

_digits = [u'一', u'二', u'三', u'四', u'五', u'六', u'七', u'八', u'九', u'十']
_months = [u'正月'] + [x+u'月' for x in _digits[1:]] + [u'冬月', u'腊月']
_days = [u'初' + x for x in _digits] + [u'十' + x for x in _digits[:-1]] + [u'二十'] + [u'廿'+x for x in _digits[:-1]] + [u'三十']

class Form(forms.Form):
    month = forms.IntegerField(label='月',
                               min_value=1,
                               max_value=12,
                               initial=1,
                               widget=forms.Select(choices=[(x,_months[x-1]) for x in range(1, 13)]),
                               )
    leapMonth = forms.BooleanField(label='闰月', required=False)
    leapMonthWa = forms.IntegerField(label='方案',
                                    initial=0,
                                    widget=forms.Select(
                                    choices=[(0, '忽略'),
                                             (1, '下一月'),
                                             (-1, '上一月')
                                             ]),
                                    help_text='闰月不存在时的解决方案',
                                    required=False,
                                    )
    day = forms.IntegerField(label='日',
                             min_value=1,
                             max_value=30,                             
                             initial=1,
                             widget=forms.Select(choices=[(x,_days[x-1]) for x in range(1, 31)]),
                             )
    lastDayWa = forms.IntegerField(label='方案',
                                   widget=forms.Select(
                                  choices=[(0, '忽略'),
                                         (1, '下一日'),
                                         (-1, '上一日')
                                         ]),
                                  help_text='三十日不存在时的解决方案',
                                  required=False,
                                  )
    beginYear = forms.IntegerField(label='起始年',
                                   min_value=1900,
                                   max_value=2050,
                                   initial=datetime.date.today().year,
                                   widget=forms.TextInput(attrs={'size':'4'}), 
                                   help_text="从 1900 到 2050")
    endYear = forms.IntegerField(label='结束年',
                                 min_value=1900,
                                 max_value=2050,
                                 initial=2050,
                                 widget=forms.TextInput(attrs={'size':'4'}),
                                 help_text="从 1900 到 2050, 不小于起始年")
    summary = forms.CharField(label='标签',
                              initial="春节",
                              help_text="生成的 .ics 文件中的标签, 比如XXX的生日"
                              )
    preview = forms.BooleanField(label='预览', initial=True, required=False,
                                 help_text='选中则在浏览器中预览结果，否则直接下载')
    
    def clean(self):
        data = self.clean_data
        if data['beginYear'] > data['endYear']:
            raise forms.ValidationError('结束年应不小于起始年')
        return data
